package com.liuheng.manager.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liuheng.manager.service.SysRoleUserService;
import com.liuheng.manager.service.SysUserService;
import com.liuheng.spzx.model.dto.system.AssginRoleDto;
import com.liuheng.spzx.model.dto.system.SysRoleDto;
import com.liuheng.spzx.model.dto.system.SysUserDto;
import com.liuheng.spzx.model.entity.system.SysRole;
import com.liuheng.spzx.model.entity.system.SysRoleUser;
import com.liuheng.spzx.model.entity.system.SysUser;
import com.liuheng.spzx.model.vo.common.Result;
import com.liuheng.spzx.model.vo.common.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("admin/system/sysUser")
public class SysUserController {
    @Autowired
    private SysUserService service;
    @Autowired
    private SysRoleUserService sysRoleUserService;
    @GetMapping ("/findByPage/{pageNum}/{pageSize}")
    public Result<Page<SysUser>> findByPage(SysUserDto sysUserDto, @PathVariable(value = "pageNum") Integer current, @PathVariable(value = "pageSize") Integer limit
            ){
        Page<SysUser> page = new Page(current,limit);
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(SysUser::getCreateTime).like(sysUserDto!=null,SysUser::getUserName,sysUserDto.getKeyword());
        Page<SysUser> page1 = service.page(page, queryWrapper);
//        List<SysRole> records = page1.getRecords();
//        System.out.println(records);
        return Result.build(page1, ResultCodeEnum.SUCCESS);

    }
    @PostMapping("/saveSysUser")
    public Result saveSysUser(@RequestBody SysUser sysUser){
        service.save(sysUser);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }
    @PutMapping("/updateSysUser")
    public Result updateSysUser(@RequestBody SysUser sysUser){
        service.updateById(sysUser);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }
    @DeleteMapping("/deleteById/{userId}")
    public Result deleteById(@PathVariable Long userId){
        service.removeById(userId);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }
    @PostMapping("/doAssgin")
    public Result doAssgin(@RequestBody AssginRoleDto assginRoleDto){
        LambdaQueryWrapper<SysRoleUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysRoleUser::getUserId,assginRoleDto.getUserId());
//        List<SysRoleUser> list = sysRoleUserService.list(queryWrapper);
        sysRoleUserService.remove(queryWrapper);
        assginRoleDto.getRoleIdList().forEach(roleId->{
            SysRoleUser sysRoleUser = new SysRoleUser();
            sysRoleUser.setUserId(assginRoleDto.getUserId());
            sysRoleUser.setRoleId(roleId);
            sysRoleUserService.save(sysRoleUser);
        });
//        List<Long> roleIdList = assginRoleDto.getRoleIdList();
//        for (int i = 0; i < roleIdList.size(); i++) {
//            Long roleId = roleIdList.get(i);
//            SysRoleUser sysRoleUser = new SysRoleUser();
//            sysRoleUser.setUserId(assginRoleDto.getUserId());
//            sysRoleUser.setRoleId(roleId);
//            sysRoleUserService.save(sysRoleUser);
//        }
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }
}
